package com.yuexunxinxi.carefree.service.a_user;


import com.yuexunxinxi.carefree.dao.AUserMapper;
import com.yuexunxinxi.carefree.entity.AUser;
import com.yuexunxinxi.carefree.entity.vo.AUserVo;
import com.yuexunxinxi.carefree.utils.IpUtil;
import com.yuexunxinxi.carefree.utils.R;
import com.yuexunxinxi.carefree.utils.SnowFlakeUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.UUID;


/**
 * 作者: 李咨辰
 * 文件名: AUserServiceImpl.java
 * 路径: com.yuexunxinxi.carefree.service.user
 * 时间: 2024-10-05 11:29
 * 版本: 1.0
 * 描述: TODO 用户
 **/
@Service
public class AUserServiceImpl implements AUserService {

    @Resource
    private AUserMapper aUserMapper;


    /**
     * 手机号密码登录
     *
     * @param phone    手机号
     * @param password 密码
     * @param request  用于获取ip
     * @return AUser对象
     */
    @Override
    public AUser doLogin(String phone, String password, HttpServletRequest request) {
        AUser aUser = aUserMapper.getUserByPhoneAndPassword(phone, password);
        if (aUser == null) {
            return null;
        }
        String ip = IpUtil.getIpByHttpServletRequest(request);
        //登录秘钥
        String secret = UUID.randomUUID().toString();
        aUserMapper.setSecret(aUser.getId(), secret, ip);
        aUser.setSecret(secret);
        aUser.setUpdateIp(ip);
        return aUser;
    }

    /**
     * 登录状态验证
     *
     * @param id     用户编号
     * @param secret 授权码
     * @return 登录对象
     */
    @Override
    public AUser loginStatus(String id, String secret, HttpServletRequest request) {
        AUser aUser = aUserMapper.getUserByIpAndSecret(id, secret);
        if (aUser != null) {
            String ip = IpUtil.getIpByHttpServletRequest(request);
            secret = UUID.randomUUID().toString();
            aUserMapper.setSecret(aUser.getId(), secret, ip);
            aUser.setSecret(secret);
            aUser.setUpdateIp(ip);
            return aUser;
        }
        return null;
    }

    /**
     * 使用手机号验证码登录
     *
     * @param phone   手机号
     * @param request 用于获取ip地址
     * @return 用户类
     */
    @Override
    public AUser captchaLogin(String phone, HttpServletRequest request) {
        AUser aUser = aUserMapper.getUserByPhone(phone);
        //ip地址
        String ip = IpUtil.getIpByHttpServletRequest(request);
        //登录秘钥
        String secret = UUID.randomUUID().toString();
        if (aUser != null) {
            aUserMapper.setSecret(aUser.getId(), secret, ip);
            aUser.setSecret(secret);
            aUser.setUpdateIp(ip);
            return aUser;
        }
        aUser = new AUser();
        long id = SnowFlakeUtil.getFlowIdInstance().nextId();
        aUser.setId(id);
        aUser.setName("网友-" + (int) ((Math.random() * 900000) + 100000));
        aUser.setAccount(String.valueOf(id));
        aUser.setPhone(phone);
        Date time = new Date();
        aUser.setCreateTime(time);
        aUser.setUpdateTime(time);
        aUser.setCreateIp(ip);
        aUser.setUpdateIp(ip);
        aUser.setSecret(secret);
        if (aUserMapper.addUser(aUser) >= 1) {
            aUser.setCreateTime(null);
            aUser.setCreateIp(null);
            return aUser;
        }
        return null;
    }

    /**
     * 修改用户
     *
     * @param aUser   用户类
     * @param request 用于获取ip
     * @return R
     */
    @Override
    public R editUser(AUserVo aUser, HttpServletRequest request) {
        if (aUser.getAccount() != null && !aUser.getAccount().equals("")) {
            AUser accountUser = aUserMapper.getUserByAccound(aUser.getAccount());
            if (accountUser != null) {
                return R.error("账号已被占用");
            }
        }
        aUser.setUpdateTime(new Date());
        aUser.setUpdateIp(IpUtil.getIpByHttpServletRequest(request));
        int i = aUserMapper.editUser(aUser);
        if (i > 0) {
            return R.ok();
        }
        return R.error("修改失败");
    }

    /**
     * 根据id获取头像
     *
     * @param id 根据id获取头像
     * @return 头像
     */
    @Override
    public String getAvatarById(Long id) {
        return aUserMapper.getAvatarById(id);
    }
}
